#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>

#define SERVER_PORT 8888

int main(int argc, char **argv)
{
	struct sockaddr_in tSocketClientAddr;
	
	int iSocketClient;
	int iRet;
	int iSendLen;
	unsigned char ucSendBuf[1000];

	if(argc != 2)
	{
		printf("Usage : %s <server_ip>\n", argv[0]);
		return -1;
	}

	iSocketClient = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == iSocketClient)
	{
		printf("socket error!\n");
		return -1;
	}

	tSocketClientAddr.sin_family = AF_INET;
	tSocketClientAddr.sin_port   = htons(SERVER_PORT);
	if(0 == inet_aton(argv[1], &tSocketClientAddr.sin_addr))
	{
		printf("invalid server_ip\n");
		return -1;
	}
	memset(tSocketClientAddr.sin_zero, 0, 8);

	iRet = connect(iSocketClient, (const struct sockaddr *)&tSocketClientAddr, sizeof(struct sockaddr));
	if(-1 == iRet)
	{
		printf("connect error\n");
		return -1;
	}

	while(1)
	{
		if(fgets(ucSendBuf, 1000, stdin))
		{
			iSendLen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0);
			if(iSendLen <= 0)
			{
				close(iSocketClient);
				return -1;
			}
		}
	}
	return 0;
}



